//====================================================================================================
// Filename:	CharacterIdle.cpp
// Created by:	Ali Talhouni
//====================================================================================================

//====================================================================================================
// Includes
//====================================================================================================

#include "CharacterIdle.h"

#include "fighter.h"

//====================================================================================================
// Class Definitions
//====================================================================================================

CharacterIdle::CharacterIdle(fighter* pOwner)
	: CharacterState(pOwner, "Idle")
{
	// Empty
}

//----------------------------------------------------------------------------------------------------

CharacterIdle::~CharacterIdle()
{
	// Empty
}

//----------------------------------------------------------------------------------------------------

void CharacterIdle::Load(FILE* pFile)
{
	//Read state amount
	int statecount=0;
	fscanf_s(pFile,"%*s %d", &statecount);
	
	

	// Load and add pngs of particular state
	for (int i = 0; i < statecount; ++i)
	{
		char buffer[128];
		fscanf_s(pFile, "%s", buffer, 128);
		mSprite.Add(buffer);
	}

	
}

//----------------------------------------------------------------------------------------------------

void CharacterIdle::Unload()
{
	mSprite.Unload();
}

//----------------------------------------------------------------------------------------------------

void CharacterIdle::Update(float deltaTime)
{
	// Update sprite
	mSprite.Update(deltaTime);

	// State transitions
	if (Input_IsKeyPressed(mpOwner->GetJabRightKey()))
	{
		mpOwner->ChangeState(AS_JabRight);
	}

	else if (Input_IsKeyPressed(mpOwner->GetJabLeftKey()))
	{
		mpOwner->ChangeState(AS_JabLeft);
	}

	else if (Input_IsKeyPressed(mpOwner->GetDodgeRightKey()))
	{
		mpOwner->ChangeState(AS_DodgeRight);
	}

	else if (Input_IsKeyPressed(mpOwner->GetDodgeLeftKey()))
	{
		mpOwner->ChangeState(AS_DodgeLeft);
	}

	else if (Input_IsKeyPressed(mpOwner->GetHookRightKey()))
	{
		mpOwner->ChangeState(AS_HookRight);
	}

	else if (Input_IsKeyPressed(mpOwner->GetHookLeftKey()))
	{
		mpOwner->ChangeState(AS_HookLeft);
	}
}

//----------------------------------------------------------------------------------------------------

void CharacterIdle::Render(const SVector2& offset)
{
	const int kTextureWidth = mSprite.GetWidth();
	const int kTextureHeight = mSprite.GetHeight();
	const SVector2 ownerPos(mpOwner->GetPosition());
	const SVector2 renderPos(ownerPos.x - (kTextureWidth * 0.5f), ownerPos.y - kTextureHeight);
	bool facingLeft = mpOwner->IsFacingLeft();
	mSprite.SetPosition(renderPos + offset);
	mSprite.SetFlipH(facingLeft);
	mSprite.Render();
}

//----------------------------------------------------------------------------------------------------

void CharacterIdle::Enter()
{
	mSprite.Play(4.0f, true);
}

//----------------------------------------------------------------------------------------------------

void CharacterIdle::Exit()
{
	mSprite.Stop();
	mSprite.Reset();
}

//----------------------------------------------------------------------------------------------------
SRect CharacterIdle::GetBoundingBox() const
{
	const SVector2 pos(mpOwner->GetPosition());
	if (mpOwner->IsFacingLeft())
	{
	return SRect
		(
			pos.x + 270.0f,
			pos.y + 0.0f,
			pos.x + 310.0f,
			pos.y + 300.0f
		);
	}
	else
	{
		return SRect
		(
			pos.x + 150.0f,
			pos.y + 0.0f,
			pos.x + 190.0f,
			pos.y + 300.0f
		);
	}
}

SRect CharacterIdle::GetDamageBox() const
{
	const SVector2 pos(mpOwner->GetPosition());
	return SRect();
}